package com.hatic.money;

import java.util.List;
import java.util.Map;

import org.jxstar.control.action.RequestContext;
import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.StringUtil;

/**
 * 投资计划完成情况统计处理类。
 *
 * @author TonyTan
 * @version 1.0, 2011-11-2
 */
public class PlanCompStatBO extends BusinessObject {
	private static final long serialVersionUID = 1L;

	//统计付款金额
	public String statComp(RequestContext request) {
		String dept_name = request.getRequestValue("dept_name");
		String stat_month = request.getRequestValue("stat_month");
		String dept_id = request.getRequestValue("dept_id");
		//String stat_user = request.getRequestValue("stat_user");
		//String stat_userid = request.getRequestValue("stat_userid");
		if (stat_month.length() == 0 || dept_id.length() == 0) {
			setMessage("缺少统计月份与部门信息！");
			return _returnFaild;
		}
		_log.showDebug("------dept_name=" + dept_name + ";dept_id=" + dept_id + ";stat_month=" + stat_month);
		
		String statYear = stat_month.split("-")[0];
		
		//先统计：0 普通计划、6 子计划、7 包内计划
		//取投资计划
		List<Map<String,String>> lsPlan = queryStatPlan(dept_id, statYear, "(bao_type > '5' or bao_type='0')");
		if (lsPlan.isEmpty()) {
			setMessage("没有找到需要统计的投资计划！");
			return _returnFaild;
		}
		
		for (int i = 0, n = lsPlan.size(); i < n; i++) {
			Map<String,String> mpPlan = lsPlan.get(i);
			String planId = mpPlan.get("plan_id");
			String projectId = mpPlan.get("project_id");
			
			//更新当前计划中的付款金额
			//到截止月份，今年一共付的款额
			double payMoney = statYearPay(projectId, stat_month);
			updatePlanMoney(planId, payMoney, stat_month);
			//截止上年度年底共付款的额度
			double sunMoney1 = lastYearPay(projectId, stat_month);
			updatePlanSunMoney1(planId, sunMoney1);
		}
		
		//统计虚拟包或大计划，它们没有付款计划，需要从子计划中统计金额
		List<Map<String,String>> lsParent = queryStatPlan(dept_id, statYear, "bao_type < '5' and bao_type >'0'");
		for (int i = 0, n = lsParent.size(); i < n; i++) {
			Map<String,String> mpPlan = lsParent.get(i);
			String planId = mpPlan.get("plan_id");
            //到截止月份，今年一共付的款额
			updateParentMoney(planId);
            //截止上年度年底共付款的额度
			updateParentSunMoney1(planId,stat_month);
		}
		
		return _returnSuccess;
	}
	
	//统计项目今年1月至今年统计月份的今年累计支付金额
	private double statYearPay(String projectId, String statMonth) {
		String sql = "select sum(pay_money1) as check_money from contract_pay " + 
			"where plan_status = '4' and project_id = ? and plan_month < ? and plan_month >= ?";
		
		//开始日期为，今年1月1日
		String year = statMonth.split("-")[0];
		String startDate = year + "-01-01";
		
		//结束日期为，统计月度的下个月第1天
		String endDate = DateUtil.dateAddMonth(statMonth, 1);
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(projectId);
		param.addDateValue(endDate);
		param.addDateValue(startDate);
		Map<String,String> mpSum = _dao.queryMap(param);
		if (mpSum.isEmpty()) return 0;
		
		String check_money = mpSum.get("check_money");
		
		return StringUtil.tonum(check_money);
	}
    //	统计项目截止上年度年底共付款的额度
	private double lastYearPay(String projectId, String statMonth) {
		String sql = "select sum(pay_money1) as check_money from contract_pay " + 
			"where plan_status = '4' and project_id = ? and plan_month < ? ";
		
		//结束日期为，今年1月1日
		String year = statMonth.split("-")[0];
		String endDate = year + "-01-01";
		
				
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(projectId);
		param.addDateValue(endDate);
		Map<String,String> mpSum = _dao.queryMap(param);
		if (mpSum.isEmpty()) return 0;
		
		String check_money = mpSum.get("check_money");
		
		return StringUtil.tonum(check_money);
	}
	
	//更新截止统计月份的今年一年累计付款tran_money1、今年预计付款money_pay1及统计月份信息
	private boolean updatePlanMoney(String planId, double payMoney, String stat_month) {
		String sql = "update money_plan set tran_money1 = ?, money_pay1 = ?, stat_month = ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(Double.toString(payMoney));
		param.addDoubleValue(Double.toString(payMoney));
		param.addDateValue(stat_month);
		param.addStringValue(planId);
		
		return _dao.update(param);
	}
    //更新项目截止上年度年底共付款的额度
	private boolean updatePlanSunMoney1(String planId, double payMoney) {
		String sql = "update money_plan set sun_money1 = ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(Double.toString(payMoney));
		param.addStringValue(planId);		
		return _dao.update(param);
	}
	
	//取当年指定单位的投资计划
	private List<Map<String,String>> queryStatPlan(String deptId, String statYear, String baoWhere) {
		String sql = "select plan_id, project_id, bao_type from money_plan " +
					 "where plan_status >= '41' and plan_status < '91' and plan_year = ? and dept_id like ?";
		if (baoWhere.length() > 0) {
			sql += " and " + baoWhere;
		}
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(statYear);
		param.addStringValue(deptId+"%");
		
		return _dao.query(param);
	}
	
	//取所有子计划的截止上年度年底共付款的额度
	private double querySubSunMoney1(String parentId) {
		String sql = "select sum(sun_money1) as sun_money1 from money_plan " +
				"where plan_status >= '41' and plan_status < '91' and plan_parentid = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(parentId);
		Map<String,String> mpSum = _dao.queryMap(param);
		if (mpSum.isEmpty()) return 0;
		
		String data = mpSum.get("sun_money1");
		
		return StringUtil.tonum(data);
	}
    //取所有子计划的截止统计月份的今年一年累计付款tran_money1
	private double querySubMoney(String parentId) {
		String sql = "select sum(tran_money1) as tran_money1 from money_plan " +
				"where plan_status >= '41' and plan_status < '91' and plan_parentid = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(parentId);
		Map<String,String> mpSum = _dao.queryMap(param);
		if (mpSum.isEmpty()) return 0;
		
		String data = mpSum.get("tran_money1");
		
		return StringUtil.tonum(data);
	}
	
	//更新父计划中的截止统计月份的今年一年累计付款tran_money1
	private boolean updateParentMoney(String parentId) {
		double sumMoney = querySubMoney(parentId);
		
		String sql = "update money_plan set tran_money1 = ?, money_pay1 = ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(Double.toString(sumMoney));
		param.addDoubleValue(Double.toString(sumMoney));
		param.addStringValue(parentId);		
		return _dao.update(param);
	}
    //更新父计划中的截止上年度年底共付款的额度
	private boolean updateParentSunMoney1(String parentId,String stat_month) {
		double sunMoney1 = querySubSunMoney1(parentId);
		
		String sql = "update money_plan set sun_money1 = ?, stat_month = ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(Double.toString(sunMoney1));
		param.addDateValue(stat_month);
		param.addStringValue(parentId);
		
		return _dao.update(param);
	}
}
